package core.modules.configuration;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ConfigurationManager {
	private static ConfigurationManager instance;
	private String configurationsFolderName = "configurations";
	private String myConfigurationFolderName = "myConfig";
	private String configurationFileExtension = ".xml";

	public static ConfigurationManager getInstance() {
		if (instance == null) {
			synchronized (ConfigurationManager.class) {
				if (instance == null)
					instance = new ConfigurationManager();
			}
		}
		return instance;
	}

	// Zwraca sciezke do configu (z poziomu aktywacji aplikacji)
	public String getConfigFilePath(String configDirectoryName, String configFileName) {
		return configurationsFolderName + File.separator + configDirectoryName
				+ File.separator + configFileName
				+ configurationFileExtension;
	}

	// Zwraca sciezke do katalogu z configiem (z poziomu aktywacji aplikacji)
	public String getConfigDirectoryPath(String configDirectoryName) {
		return configurationsFolderName + File.separator + configDirectoryName;
	}
	
	// konstrukcja identificationField/identificationField
	public String saveConfigToFile(String configDirectoryName, String configFileName, String config)
			throws IOException {
		// Tworzymy katalog + ewentualnie
		File file = new File(getConfigDirectoryPath(configDirectoryName));
		file.mkdirs();

		// Zapisujemy do pliku konfiguracje
		BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
				getConfigFilePath(configDirectoryName, configFileName)));
		bufferedWriter.write(config);
		bufferedWriter.close();

		return getConfigFilePath(configDirectoryName, configFileName);
	}

	public String loadConfigFromFile(String configDirectoryName, String configFileName) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(getConfigFilePath(configDirectoryName, configFileName)));
		
		StringBuilder stringBuilder = new StringBuilder();
		String line = "";
		
		while ((line = reader.readLine()) != null) {
			stringBuilder.append(line);
			stringBuilder.append(System.lineSeparator());
		}
		reader.close();
		
		return stringBuilder.toString();
	}

	public void removeConfig(String identificationField) {
		removeConfig(identificationField, identificationField);
	}
	
	// Usuwa config i katalog, o ile jest w nim tylko config
	public void removeConfig(String configDirectoryName, String configFileName) {
		String filePath = getConfigFilePath(configDirectoryName, configFileName);

		File configFile = new File(filePath);
		File configDirectory = configFile.getParentFile();

		if (configFile.exists() && configFile.isFile()) {
			configFile.delete();
			if (configDirectory.exists() && configDirectory.isDirectory()
					&& configDirectory.list().length == 0) {
				configDirectory.delete();
			}
		}
	}
	
	

	public String getConfigurationsFolderName() {
		return configurationsFolderName;
	}

	public String getMyConfigurationFolderName() {
		return myConfigurationFolderName;
	}
}
